package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.StrategyReview;
import cn.wolfcode.trip.base.mapper.StrategyReviewMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IStrategyReviewService;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class StrategyReviewServiceImpl implements IStrategyReviewService {

    @Autowired
    private StrategyReviewMapper strategyReviewMapper;

    // 保存点评
    @Override
    public void save(StrategyReview strategyReview) {
        strategyReview.setCreateTime(new Date());
        strategyReview.setFavs(0);
        strategyReview.setUser(UserContext.getCurrentUser());
        if (strategyReview.getRating() == null) {
            strategyReview.setRating(3);
        }
        strategyReviewMapper.insert(strategyReview);
    }

    // 分页查询
    @Override
    public PageInfo<StrategyReview> query(Long sid, QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), "favs desc");
        List<StrategyReview> list = strategyReviewMapper.selectForList(sid, qo);
        return new PageInfo<>(list);
    }

    @Override
    public List<StrategyReview> listAll(Long sid) {
        return strategyReviewMapper.selectAll(sid);
    }

    // 取消攻略点评赞
    @Override
    public void delSFav(Long uid, Long sid) {
        strategyReviewMapper.delSFav(sid);
        strategyReviewMapper.remSRFollow(uid, sid);
    }

    // 赞攻略点评
    @Override
    public void addSFav(Long uid, Long sid) {
        strategyReviewMapper.addSFav(sid);
        strategyReviewMapper.insSRFollow(uid, sid);
    }

    // 用户是否有赞攻略点评
    @Override
    public StrategyReview getFavSRById(Long uid, Long sid) {
        return strategyReviewMapper.getFavSRById(uid, sid);
    }
}
